﻿using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Castle.DynamicProxy.Generators;
using MediatR;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Traceability.Register.Application.Commonds.TraceabilityManagementCommonds;
using Traceability.Register.Domain;
using Traceability.Register.ErrorCode.DTOS;
using Traceability.Register.ErrorCode.Result;
using Traceability.Register.Infrastructrue;

namespace Traceability.Register.Application.CommondHandlers.TraceabilityManagementCommondHandlers
{
    /// <summary>
    /// 溯源管理显示请求命令处理
    /// </summary>
    public class TraceabilityManagementShowCommondHandler : IRequestHandler<TraceabilityManagementShowCommond, ApiResult>
    {
        /// <summary>
        /// 商品信息仓储
        /// </summary>
        private readonly IBaseRepository<Goods> baseRepository;
        /// <summary>
        /// 溯源信息仓储
        /// </summary>
        private readonly IBaseRepository<Traceabilitys> repository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<TraceabilityManagementShowCommond> logger;
        private readonly IMemoryCache memoryCache;


        public TraceabilityManagementShowCommondHandler(IBaseRepository<Goods> baseRepository, IBaseRepository<Traceabilitys> repository, ILogger<TraceabilityManagementShowCommond> logger, IMemoryCache memoryCache)
        {
            this.baseRepository = baseRepository;
            this.repository = repository;
            this.logger = logger;
            this.memoryCache = memoryCache;
        }



        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回结果</returns>
        public async Task<ApiResult> Handle(TraceabilityManagementShowCommond request, CancellationToken cancellationToken)
        {
            try
            {
                
                var goodbase = await baseRepository.GetAllAsync();
                var goods = await repository.QueryAsync(x => x.IsDeleted == false);
                var entity = from a in goodbase
                             join b in goods
                             on a.GoodId equals b.GoodId
                             where b.IsDeleted == false
                             select new TraceabilitysDTO
                             {
                                 GoodId = a.GoodId,
                                 GoodName = a.GoodName,
                                 TraceabilityId = b.TraceabilityId,
                                 ProductionBatch = b.ProductionBatch,
                                 ExprirationDate = b.ExprirationDate,
                                 ProductionLicense = b.ProductionLicense,
                                 ProductionEnterprise = b.ProductionEnterprise,
                                 GoverImarger = b.GoverImarger,
                             };
                var page = new PageResult()
                {
                    Data = entity.Skip(request.PageSize*(request.PageIndex-1)).Take(request.PageSize).ToList(),
                    RecourdCount = entity.Count(),
                    PageCount=(int)Math.Ceiling(entity.Count()/(double)request.PageSize)
                };
               
                return ApiResult.Ok(page);
            }
            catch (Exception ex)
            {
                logger.LogError("查询失败" + ex.Message);
                throw;
            }
        }
    }
}
